#include "PersonalSpeachTimerDlg.h"
#include "ui_PersonalSpeachTimerDlg.h"
#include <QDateTime>
#include <QMessageBox>
#include "PersonalSpeachTimerOverDlg.h"


#define ForTestTimter 0
#if ForTestTimter
#   define ShowSubDialog show //showFullScreen
#else
#   define ShowSubDialog showFullScreen //showFullScreen
#endif

//---------------------------------------------------------
PersonalSpeachTimerDlg::PersonalSpeachTimerDlg(QWidget *parent)
    : QDialog(parent)
    , m_Step(SpeachFinished)
    , ui(new Ui::PersonalSpeachTimerDlg)
    , m_DelayCount(0)
    , m_pTimeOverDlg(NULL)
    , m_PunishTarget(10)
    , m_Punish(0)

{
    ui->setupUi(this);
    setWindowOpacity(0.9);

    m_pTimer = new QTimer(this);
    m_pause = false;

    ui->pushButtonPause->setEnabled(false);
    ui->labelPunish->hide();//hide

    m_pTimeOverDlg = new PersonalSpeachTimerOverDlg();
    m_pTimeOverDlg->hide();

    //for test only
#if ForTestTimter
    ui->doubleSpinBoxTimeCount->setValue(0.1);//for test
    ui->doubleSpinBoxDelayCount->setValue(0.1);//for test
    ui->doubleSpinBoxWholeCount->setValue(0.5);//for test
#endif
    InitNumber();//show time

    connect(m_pTimer, SIGNAL(timeout()), this, SLOT(SubTime()));

    connect(ui->doubleSpinBoxWholeCount,SIGNAL(valueChanged(double)),this, SLOT(InitNumber()));
    connect(ui->pushButtonStart, SIGNAL(clicked()), this, SLOT(Start()));
    connect(ui->pushButtonPause, SIGNAL(clicked()), this, SLOT(NextStep()));
    connect(ui->pushButtonReset, SIGNAL(clicked()), this, SLOT(OnFinishSpeach()));
    connect(ui->pushButtonClose, SIGNAL(clicked()), this, SLOT(OnClose()));

    connect(m_pTimeOverDlg, SIGNAL(sigStartTalking()), this, SLOT(OnStartTalking()));
    connect(m_pTimeOverDlg, SIGNAL(sigFinishSpeach()), this, SLOT(OnFinishSpeach()));
    connect(m_pTimeOverDlg, SIGNAL(sigPause()), this, SLOT(Pause()));

}
//------------------------------------------------
PersonalSpeachTimerDlg::~PersonalSpeachTimerDlg()
{
    delete ui;
    delete m_pTimer;
    delete m_pTimeOverDlg;//delete
}
//------------------------------------------------
void PersonalSpeachTimerDlg::InitNumber()
{
    SetLCDNumColor("color: rgb(0,0,0)");
    SetTimeCount(ui->doubleSpinBoxTimeCount->value());
    SetTimeDelay(ui->doubleSpinBoxDelayCount->value());
    SetTimeWhole(ui->doubleSpinBoxWholeCount->value());
    m_PunishTarget = ui->doubleSpinBoxPunish->value();//set punish
    m_DelayCountTotal = m_DelayCount;//copy
    m_Punish = 0.0;//set punish
    ShowTime();
}
//------------------------------------------------
void PersonalSpeachTimerDlg::mousePressEvent(QMouseEvent *event)
{
    this->dPos = event->globalPos() - this->pos();
}
//------------------------------------------------
void PersonalSpeachTimerDlg::mouseMoveEvent(QMouseEvent *event)
{
    m_PosX = this->pos().x();//x pos
    this->move(event->globalPos() - this->dPos);
}
//------------------------------------------------
void PersonalSpeachTimerDlg::NextStep()
{
    m_pTimeOverDlg->ShowSubDialog();//show information dialog
}

//------------------------------------------------
void PersonalSpeachTimerDlg::OnClose()
{
    this->hide();
    m_pTimeOverDlg->hide();
    this->close();
}
//------------------------------------------------
void PersonalSpeachTimerDlg::OnFinishSpeach()
{
    m_Step = SpeachFinished;
    m_pTimer->stop();
    InitNumber();
    ui->pushButtonStart->setEnabled(true);
    ui->pushButtonPause->setEnabled(false);
    ui->doubleSpinBoxTimeCount->setEnabled(true);//enable
    ui->doubleSpinBoxDelayCount->setEnabled(true);//enable
    ui->doubleSpinBoxWholeCount->setEnabled(true);//enable
    ui->doubleSpinBoxPunish->setEnabled(true);//enable

    ui->widget->setVisible(true);
    this->show();
}
//------------------------------------------------
void PersonalSpeachTimerDlg::OnStartTalking()
{
    m_Step = SpeachTalking;
    ShowTime();
    ui->pushButtonStart->setEnabled(false);
    ui->pushButtonPause->setEnabled(true);
    ui->widget->setVisible(false);
}
//------------------------------------------------
void PersonalSpeachTimerDlg::Pause()
{
    m_pause = true;
    m_pTimer->stop();//stop
    ui->pushButtonStart->setEnabled(true);
    ui->pushButtonPause->setEnabled(false);
    ui->widget->setVisible(true);
}
//------------------------------------------------
void PersonalSpeachTimerDlg::reject()
{
    this->show();
}
//------------------------------------------------
void PersonalSpeachTimerDlg::SetLCDNumColor(const QString & iColor)
{
    ui->labelTime->setStyleSheet(iColor);
}
//------------------------------------------------
void PersonalSpeachTimerDlg::SetTimeCount(double time)
{
    m_TimeCount = time * 60;
    if(m_TimeCount>0)    m_Step = SpeachFirst;//first
 }
//------------------------------------------------
void PersonalSpeachTimerDlg::SetTimeDelay(double time)
{
    m_DelayCount = time * 60;
}
//------------------------------------------------
void PersonalSpeachTimerDlg::SetTimeWhole(double time)
{
    m_WholeCount = time * 60;
}
//------------------------------------------------
void PersonalSpeachTimerDlg::ShowTime()
{
    int time = 0;//time for show
    //swithe the type of step
    switch(m_Step)
    {
    case SpeachFirst://First
        {
            time = m_TimeCount;
            if(time == 59||time == 57||time == 55
                    ||time == 53||time == 51)
            {
                SetLCDNumColor("background-color: rgb(255,0,0)");
            }
            else if(time == 58||time == 56||time == 54
                    ||time == 52||time == 50)
            {
                SetLCDNumColor("background-color: rgb(255,255,255)");
            }
            break;
        }
    case SpeachDelay://Delay
        {
            time = m_DelayCount;
            if ( time%2 == 0)
            {
                SetLCDNumColor("background-color: rgb(255,255,0)");
            }
            else
            {
                SetLCDNumColor("background-color: rgb(255,255,200)");
            }

            break;
        }
    default://talking
        {
            SetLCDNumColor("background-color: rgb(255,255,255)");
             time = m_WholeCount;
            break;
        }//default
    }//switch

    if(m_Punish>=0.1)
    {
       QString punishStr = QString::number(m_Punish,'f',1);
       ui->labelPunish->setText(punishStr);
    }
    if (time<0) return;//error

    int m = time / 60;
    int s = time % 60;
    QString timeStr = "%1:%2";
    if (m/10 == 0 && s/10 == 0) timeStr = "0%1:0%2";
    else if (m/10 == 0 && s/10 !=0) timeStr =  "0%1:%2";
    else if (m/10 != 0 && s/10 == 0) timeStr =   "%1:0%2";
    ui->labelTime->setText(timeStr.arg(m).arg(s));
}
//------------------------------------------------
void PersonalSpeachTimerDlg::Start()
{
    m_PosX = this->pos().x();//x pos

    if(m_WholeCount< m_TimeCount + m_DelayCount)
    {
        m_WholeCount = m_TimeCount + m_DelayCount;
    }
    ui->widget->setVisible(false);
    if(m_pause)
    {
        m_pause = false;
    }
    else
    {
        ui->labelPunish->hide();//hide
        InitNumber();//show time
    }

    //for buttons active
    if(!m_pTimer->isActive())
    {
        m_pTimer->start(1000);
    }
    ui->pushButtonStart->setEnabled(false);
    ui->pushButtonPause->setEnabled(true);

    ui->doubleSpinBoxTimeCount->setEnabled(false);//disable
    ui->doubleSpinBoxDelayCount->setEnabled(false);//disable
    ui->doubleSpinBoxWholeCount->setEnabled(false);//disable
    ui->doubleSpinBoxPunish->setEnabled(false);//disable

    this->hide();
    setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
    this->show();
    this->move(m_PosX,0);
}
//------------------------------------------------
void PersonalSpeachTimerDlg::SubTime()
{
    //swithe the type of step
    switch(m_Step)
    {
    case SpeachFirst://First
        if(m_TimeCount<1)
        {
            if(m_DelayCountTotal>0)
            {
                m_Step = SpeachDelay;//next step
            }
            else
            {
                m_Step = SpeachTalking;//next step
                break;
            }
        }
        else
        {
            m_TimeCount--;//time--
            break;
        }
    case SpeachDelay://Delay
        {
            m_DelayCount--;//time--
            m_Punish = m_PunishTarget * (m_DelayCountTotal - m_DelayCount)/m_DelayCountTotal;
            if(m_DelayCount <= 0)
            {
                m_Step = SpeachTalking;//next step
                m_pTimeOverDlg->ShowSubDialog();//show information dialog
            }
            break;
        }
    case SpeachTalking://talking
        if(m_WholeCount > 0)
        {
            break;
        }
    default:
        m_Step = SpeachFinished;//next step
        m_pTimer->stop();
        m_pTimeOverDlg->ShowSubDialog();//show information dialog
        break;
    }//switch
    ui->labelPunish->setVisible(m_Punish>0);
    m_WholeCount --;//whole --
    ShowTime();
}

